package com.ciei.dpagm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.controller.BaseController;
import com.ciei.dpagm.entity.Supplier;
import com.ciei.dpagm.entity.Users;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.JsonUtil;
import com.ciei.dpagm.util.PageParam;
import com.ciei.dpagm.service.SupplierService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * @author pp
 * @date 2020/8/31 11:13
 **/
@Controller
public class SupplierController extends BaseController {
    @Autowired
    private SupplierService supplierService;
    @Value("${baseFilePath}")
    private  String  baseFilePath;

    @RequestMapping("vue/supplier/load")
    @ResponseBody
    public JSONObject loadSupplierDataVue(@RequestBody JSONObject page) {
        PageParam ps=getPageParams(page);
        return supplierService.loadSupplieDataVue(ps);
    }

    /**
     * 供应商管理-新增
     */
    @RequiresPermissions("system:supplier:add")
    @RequestMapping("vue/supplier/add")
    @ResponseBody
    public JSONObject addSupplierVue(@RequestBody JSONObject jsonObject) {
        Users user = (Users) SecurityUtils.getSubject().getPrincipal();
        return supplierService.addOrUpdateSupplie(jsonObject,"add",user);
    }

    /**
     * 供应商管理-修改
     */
    @RequiresPermissions("system:supplier:edit")
    @RequestMapping("vue/supplier/update")
    @ResponseBody
    public JSONObject updateSupplierVue(@RequestBody JSONObject jsonObject) {
        Users user = (Users) SecurityUtils.getSubject().getPrincipal();
        return supplierService.addOrUpdateSupplie(jsonObject,"edit",user);
    }

    /**
     * 供应商管理-删除
     */
    @RequiresPermissions("system:supplier:del")
    @RequestMapping("vue/supplier/delete")
    @ResponseBody
    public JSONObject deleteSupplierVue(@RequestBody JSONObject jsonObject) {
        return supplierService.deleteVue(jsonObject);
    }

    /**
     * 供应商管理-导出选中
     */
    @RequiresPermissions("system:supplier:export")
    @RequestMapping("vue/supplier/export")
    @ResponseBody
    public JSONObject exportVue(@RequestBody JSONArray jsonArray) {
        return supplierService.exportVue(baseFilePath,jsonArray);
    }

    /**
     * 供应商管理-导出全部
     */
    @RequiresPermissions("system:supplier:export")
    @RequestMapping("vue/supplier/exportAll")
    @ResponseBody
    public JSONObject exportAllVue() {
       return supplierService.exportAllVue(baseFilePath);
    }

    /**
     * 供应商管理-附件列表
     */
    @RequiresPermissions("system:supplier:file")
    @RequestMapping("vue/supplier/loadFile")
    @ResponseBody
    public JSONObject loadFileVue(@RequestBody JSONObject page) {
        PageParam ps=getPageParams(page);
        return supplierService.loadFileVue(ps);
    }
    @RequestMapping("supplier/findAllSupplier")
    @ResponseBody
    public JSONObject findAllSupplier(){
        List<Supplier> supplierList = supplierService.findAll();
        JSONArray array = new JSONArray();
        if (supplierList != null && supplierList.size()>0){
            for (Supplier supplier:supplierList){
                HashMap<String, Object> map = new HashMap<>();
                map.put("value",supplier.getId());
                map.put("text",supplier.getName());
                array.add(map);
            }
        }
        JSONObject json = JsonUtil.getSuccess("");
        json.put(APP.DATA,array);
        return json;
    }
}
